package controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Attendant;
import data.*;
import spark.Request;
import spark.Response;

public class ReportController extends BaseController {
	private IReportDAO dao;
	private ICourseDAO courseDao;
	public ReportController(Request request, Response response) {
		super(request, response);
		
		dao = new ReportDAO();
		courseDao = new CourseDAO();
	}
	
	public Object generateIndividualReport(){
		Map<String, Object> attributes = new HashMap<>();
		if(request.requestMethod().toUpperCase().equals("GET")){
		attributes.put("students", courseDao.listStudents());
		attributes.put("studentId", 0);
		
		}else {
			int studentId = Integer.parseInt( request.queryParams("studentId"));
			List<Attendant> attendants = dao.listAttendants(studentId);
			attributes.put("studentId", studentId);
			attributes.put("attendants", attendants);
			attributes.put("students", courseDao.listStudents());
		}
		
		return render(attributes, "report/student_report.html");
	}
	
	public Object calculateCourseAttendantRate(){
		Map<String, Object> attributes = new HashMap<>();
		if(request.requestMethod().toUpperCase().equals("GET")){
			attributes.put("courses", courseDao.list());
			attributes.put("courseId", 0);
		}else {
			attributes.put("courses", courseDao.list());
			int courseId = Integer.parseInt( request.queryParams("courseId"));
			attributes.put("courseId", courseId);
			List<Attendant> attendants = dao.listCourseAttendants(courseId);
			int count = 0;
			for(Attendant a : attendants){
				if(a.getStatus() == 0)
					count++;
			}
			float rate = (float) ((count * 1.0) / attendants.size());
			attributes.put("rate", rate);
		}
		return render(attributes, "report/course_report.html");
	}
	
	

}
